package com.jiayi.cms.service;

import java.util.List;

import com.doudou.core.container.ArrUtil;
import com.jiayi.common.utils.PageUtils;
import com.jiayi.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jiayi.common.core.page.TableDataInfo;
import com.jiayi.cms.mapper.JiayiNewsTypeMapper;
import com.jiayi.data.entity.schema.JiayiNewsType;
import com.jiayi.data.entity.excel.JiayiNewsTypeExcel;
import com.jiayi.data.entity.bo.JiayiNewsTypeBO;
import com.jiayi.data.entity.request.JiayiNewsTypeRequest;
import com.jiayi.data.entity.request.JiayiNewsTypeSearchRequest;
import com.jiayi.data.entity.request.JiayiNewsTypeSearchPageRequest;
import com.jiayi.data.entity.response.JiayiNewsTypeDTO;
import com.jiayi.data.converter.JiayiNewsTypeConverter;
import com.jiayi.api.JiayiNewsTypeService;
import com.jiayi.common.developutil.ConvertUtils;
import com.github.pagehelper.PageInfo;
import com.jiayi.common.utils.DateUtils;
import javax.servlet.http.HttpServletResponse;
import com.jiayi.common.constant.HttpStatus;
import static com.jiayi.common.utils.SecurityUtils.getUserIdStr;

/**
 * 新闻动态类型Service业务层处理
 *
 * @author zzx
 * @date 2024-11-26
 */
@Service
@Slf4j
public class JiayiNewsTypeServiceImpl implements JiayiNewsTypeService {
    @Autowired
    private JiayiNewsTypeMapper jiayiNewsTypeMapper;

    @Autowired
    private JiayiNewsTypeConverter jiayiNewsTypeconverter;

    /**
     * 查询新闻动态类型
     *
     * @param id 新闻动态类型主键
     * @return 新闻动态类型
     */
    @Override
    public JiayiNewsTypeDTO selectJiayiNewsTypeById(Long id) {
        JiayiNewsType jiayiNewsType = jiayiNewsTypeMapper.selectJiayiNewsTypeById(id);
        return jiayiNewsTypeconverter.convertToJiayiNewsTypeDTO(jiayiNewsType);
    }

    /**
     * 查询新闻动态类型列表
     *
     * @param request 新闻动态类型
     * @return 新闻动态类型
     */
    @Override
    public TableDataInfo selectJiayiNewsTypeList(JiayiNewsTypeSearchPageRequest request) {
        PageUtils.startPage(request.getPageNum(),request.getPageSize());
        JiayiNewsType jiayiNewsType = jiayiNewsTypeconverter.convertToJiayiNewsType(request);
        List<JiayiNewsType> list = jiayiNewsTypeMapper.selectJiayiNewsTypeList(jiayiNewsType);
        List<JiayiNewsTypeDTO> dtos = ConvertUtils.convert(list, jiayiNewsTypeconverter::convertToJiayiNewsTypeDTO);
        return TableDataInfo.builder().code(HttpStatus.SUCCESS).msg("查询成功").rows(dtos).total(new PageInfo(list).getTotal()).build();
    }

    /**
     * 查询新闻动态类型列表 不分页
     *
     * @param request 新闻动态类型
     * @return 新闻动态类型集合
     */
    public List<JiayiNewsType> selectJiayiNewsTypeList(JiayiNewsTypeSearchRequest request){
        JiayiNewsType jiayiNewsType = jiayiNewsTypeconverter.convertToJiayiNewsType(request);
        List<JiayiNewsType> list = jiayiNewsTypeMapper.selectJiayiNewsTypeList(jiayiNewsType);
        return list;
    }

    @Override
    public List<JiayiNewsTypeDTO> selectJiayiNewsTypeListByIds(Long...ids) {
        List<JiayiNewsType> list = jiayiNewsTypeMapper.selectJiayiNewsTypeListByIds(ids);
        return ConvertUtils.convert(list, jiayiNewsTypeconverter::convertToJiayiNewsTypeDTO);
    }

    /**
     * excel 导出 全量
     *
     * @param request 新闻动态类型
     */
    public void export(JiayiNewsTypeSearchRequest request, HttpServletResponse response){
        JiayiNewsType jiayiNewsType = jiayiNewsTypeconverter.convertToJiayiNewsType(request);
        List<JiayiNewsType> list = jiayiNewsTypeMapper.selectJiayiNewsTypeList(jiayiNewsType);
        List<JiayiNewsTypeExcel> excelList = ConvertUtils.convert(list, jiayiNewsTypeconverter::convertToJiayiNewsTypeExcel);
        ExcelUtil<JiayiNewsTypeExcel> util = new ExcelUtil<JiayiNewsTypeExcel>(JiayiNewsTypeExcel.class);
        util.exportExcel(response, excelList, "新闻动态类型数据");
    }

    /**
     * 新增新闻动态类型
     *
     * @param request 新闻动态类型
     * @return 结果
     */
    @Override
    public int insertJiayiNewsType(JiayiNewsTypeRequest request) {
        JiayiNewsType jiayiNewsType = jiayiNewsTypeconverter.convertToJiayiNewsType(request);
        jiayiNewsType.setCreateBy(getUserIdStr());
        jiayiNewsType.setCreateTime(DateUtils.getNowDate());
        return jiayiNewsTypeMapper.insertJiayiNewsType(jiayiNewsType);
    }

    /**
     * 修改新闻动态类型
     *
     * @param request 新闻动态类型
     * @return 结果
     */
    @Override
    public int updateJiayiNewsType(JiayiNewsTypeRequest request) {
        JiayiNewsType jiayiNewsType = jiayiNewsTypeconverter.convertToJiayiNewsType(request);
        return jiayiNewsTypeMapper.updateJiayiNewsType(jiayiNewsType);
    }

    /**
     * 批量删除新闻动态类型
     *
     * @param ids 需要删除的新闻动态类型主键
     * @return 结果
     */
    @Override
    public int deleteJiayiNewsTypeByIds(Long[] ids) {
        return jiayiNewsTypeMapper.deleteJiayiNewsTypeByIds(ids);
    }

    /**
     * 删除新闻动态类型信息
     *
     * @param id 新闻动态类型主键
     * @return 结果
     */
    @Override
    public int deleteJiayiNewsTypeById(Long id) {
        return jiayiNewsTypeMapper.deleteJiayiNewsTypeById(id);
    }
}
